package com.yfj.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yfj.entity.Course;
import com.yfj.service.ManagerService;
import com.yfj.service.StudentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StudentController {

    @Autowired
    StudentService studentService;
    @Autowired
    ManagerService managerService;

    /**
     * 页面跳转
     *
     * @return
     */
    @RequiresRoles("student")
    @RequestMapping({"/student", "/myAllCourse"})
    public String myAllCourse() {
        return "student/myAllCourse";
    }

    @RequiresRoles("student")
    @RequestMapping("/myChose")
    public String myChoseCourse() {
        return "student/myChose";
    }

    @RequiresRoles("student")
    @RequestMapping("/myCourseList")
    public String myCourseList() {
        return "student/myCourseList";
    }
    //--------------------------------------------------------------------------------------------------

    /*
     * 查询或显示全部
     */


    //选课
    @RequiresRoles("student")
    @RequiresPermissions("course:chose")
    @RequestMapping("/choseCourse")
    @ResponseBody
    public Map<String, Object> choseCourse(Long id, Long myId) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            studentService.choseCourse(id, myId);
            map.put("mark", 1);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("mark", 0);
        }
        return map;
    }

    //--------------------------------------------------------------------------------------------------
    //查找某studentId下的课程
    @RequiresRoles("student")
    @RequestMapping("/getMyCourse")
    @ResponseBody
    public List<Course> getMyCourse(Long myId) {
        List<Course> cList = studentService.getMyCourse(myId);
        return cList;
    }

    //--------------------------------------------------------------------------------------------------
    //取消选课
    @RequiresRoles("student")
    @RequiresPermissions("course:cancleCourse")
    @RequestMapping("/cancelCourse")
    @ResponseBody
    public int cancelCourse(Long id, Long myId) {
        int mark = 0;
        try {
            studentService.cancelCourse(id, myId);
            mark = 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mark;
    }
}
